package com.ywicc.commlibs.socket;

import android.util.Log;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;

/**
 * author : zhouxuan77
 * e-mail : zhouxuan77@163.com
 * time   : 2022/03/17
 * desc   : Socket 常用方法封装
 * version: 1.0
 */
public class SocketUtils {

    // 指定 Socket 通讯的默认端口
    public final static int IPPort = 60000;

    // Socket 严格模式 (在发送的内容前，先发送需要发送数据的长度)
    public final static int SERVER_RECEIVE_NORMAL = 1;
    public final static int SERVER_RECEIVE_STRICT = 2;

    private final String TAG = "SocketUtils";

    /**
     * Socket 接收数据
     *
     * @param socket   Socket 对象
     * @param isStrict 是否处于严格模式
     * @return 返回接收的数据
     */
    @SuppressWarnings("all")
    public byte[] receiveData(Socket socket, boolean isStrict) {
        // 判断 Socket 是否处于关闭状态
        if (socket != null) {
            try {
                DataInputStream inputStream = new DataInputStream(socket.getInputStream());

                byte[] receiveData;

                if (isStrict) {
                    // 先获取需要接收的数据的长度
                    byte[] dataLength = new byte[1];
                    inputStream.read(dataLength, 0, 1);

                    Log.w(TAG, "dataLength: " + dataLength[0]);

                    // 根据数据长度接收数据
                    receiveData = new byte[dataLength[0]];
                    inputStream.read(receiveData, 0, dataLength[0]);

                    Log.w(TAG, "receiveData: " + Arrays.toString(receiveData));
                } else {
                    // 读取缓冲区的全部数据
                    receiveData = new byte[inputStream.available()];
                    inputStream.read(receiveData);

                    Log.w(TAG, "Receive data: " + Arrays.toString(receiveData));
                }
                return receiveData;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    /**
     * Socket 发送数据
     *
     * @param socket   Socket 对象
     * @param sendData 需要发送的数据 (byte[])
     * @param isStrict 是否处于严格模式
     */
    public void sendData(Socket socket, byte[] sendData, boolean isStrict) {
        if (socket != null) {
            byte[] tmpSendData;

            if (isStrict) {
                // 在发送的数据前加上需要发送的数据长度
                tmpSendData = new byte[sendData.length + 1];
                tmpSendData[0] = (byte) sendData.length;
                System.arraycopy(sendData, 0, tmpSendData, 1, sendData.length);
            } else {
                tmpSendData = sendData;
            }

            try {
                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

                outputStream.write(tmpSendData, 0, tmpSendData.length);
                outputStream.flush();

                Log.w(TAG, "Send data: " + Arrays.toString(tmpSendData));
                Log.w(TAG, "Send data succeed");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
